/**
 * 
 */
package org.yelongframework.spring.web.servlet.mvc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.yelongframework.servlet.http.CookieSupport;
import org.yelongframework.servlet.http.HttpServletRequestIPResolver;
import org.yelongframework.servlet.http.HttpServletRequestSupport;
import org.yelongframework.servlet.http.HttpServletResponseSupport;
import org.yelongframework.servlet.mvc.AbstractController;
import org.yelongframework.spring.web.multipart.MultipartRequestSupport;
import org.yelongframework.spring.web.servlet.SpringWebMvcSupport;

/**
 * 抽象SpringMvc控制器。提供一些SpringMvc中常用功能性的方法
 * 
 * @since 1.0.0
 */
public abstract class AbstractSpringMvcController extends AbstractController implements SpringMvcController {

	@Resource
	protected SpringWebMvcSupport springWebMvcSupport;

	@Resource
	protected MultipartRequestSupport multipartRequestSupport;

	@Resource
	protected HttpServletRequestSupport httpServletRequestSupport;
	
	@Resource
	protected CookieSupport cookieSupport;
	
	@Resource
	protected HttpServletResponseSupport httpServletResponseSupport;
	
	@Resource
	protected HttpServletRequestIPResolver httpServletRequestIPResolver;
	
	@Resource
	private HttpServletResponse response;

	// ==================================================请求和响应==================================================

	@Override
	public HttpServletRequest getRequest() {
		return springWebMvcSupport.getRequest();
	}

	@Override
	public HttpServletResponse getResponse() {
		return response;
	}

	// ==================================================请求文件==================================================

	public MultipartFile getMultipartFile(String name) {
		return multipartRequestSupport.getMultipartFile(getRequest(), name);
	}

	public MultipartRequest getMultipartRequest() {
		return multipartRequestSupport.getMultipartRequest(getRequest());
	}

	// ==================================================重定向==================================================

	/**
	 * 重定向到一个新的页面
	 * 
	 * @param view 重定向到的视图
	 * @return RequestMapping方法返回的字符串
	 */
	public String redirect(String view) {
		return "redirect:" + view;
	}

	// ==================================================支持==================================================

	@Override
	public HttpServletRequestSupport getRequestSupport() {
		return httpServletRequestSupport;
	}

	@Override
	public CookieSupport getCookieSupport() {
		return cookieSupport;
	}

	@Override
	public HttpServletResponseSupport getResponseSupport() {
		return httpServletResponseSupport;
	}

	@Override
	public HttpServletRequestIPResolver getRequestIPResolver() {
		return httpServletRequestIPResolver;
	}

}
